﻿using UnityEngine;
using System.Collections.Generic;
using System;

namespace SocialPoint.Examples.MVC {

    /// <summary>
    /// 控制所有棋子GameObject的显示，隐藏
    /// </summary>
    public class ChessViewPresenter : MonoSingleton<ChessViewPresenter> {

        /// <summary>
        /// 选中的棋子GameObject
        /// </summary>
        static GameObject s_selectedChess;
        public static GameObject SelectedChess {
            get {
                return s_selectedChess;
            }
            set {
                ResetSelectedFX( );
                s_selectedChess = value;
                SelectedFX( );
            }
        }

        static void SelectedFX ( ) {
            if (null != s_selectedChess) {
                s_selectedChess.renderer.material.color = Color.red;
            }        
        }

        public static void ResetSelectedFX ( ) {
            if (null != s_selectedChess) {
                s_selectedChess.renderer.material.color = Color.white;
            }            
        } 

        /// <summary>
        /// 【棋盘256下标——在该检查球上显示的棋子GameObject，如果没有则是NULL】
        /// </summary>
        Dictionary<int, GameObject> m_hashIndexCheess = new Dictionary<int, GameObject>( );


        Dictionary<int, GameObject> m_hashChessTypeValueGo = new Dictionary<int, GameObject>( );
        /// <summary>
        /// 【棋子类型值——棋子模型GameObject】所有棋子类型GameObject
        /// </summary>
        public Dictionary<int, GameObject> HashChessTypeValueGo {
            get {
                return m_hashChessTypeValueGo;
            }
        }

        #region Unity3D Messages propagation

        protected void Start ( ) {
            Init( );
        }

        #endregion

        void Init ( ) {
            GameObject[ ] cheessGos = GameObject.FindGameObjectsWithTag(MonoConst.TAG_PLAYER);
            // 所有棋子类型GameObject放到HASH里去
            InitChessTypeHash(cheessGos);

            InitHashIndexCheess( );
        }

        // 所有棋子类型GameObject放到HASH里去
        void InitChessTypeHash ( GameObject[ ] cheessGos ) {
            foreach ( GameObject chess in cheessGos ) {
                // 直接用的GAME OBJ的名字当KEY
                int chessTypeValue = int.Parse(chess.name);
                if ( !HashChessTypeValueGo.ContainsKey(chessTypeValue) ) {
                    HashChessTypeValueGo.Add(int.Parse(chess.name), chess);
                }
            }
        }

        /// <summary>
        /// 得到该棋子类型的棋子GameObject
        /// </summary>
        /// <param name="chessTypeValue">棋子类型值</param>
        /// <returns>该棋子类型的棋子GameObject</returns>
        public GameObject GetChessTypeGo ( int chessTypeValue ) {
            GameObject chess = null;
            if (0 != chessTypeValue) {
                if (HashChessTypeValueGo.TryGetValue(chessTypeValue, out chess)) {
                    // 复制一个出来用
                    return Instantiate(chess) as GameObject;
                } else {
                    Debuger.LogError(string.Format("Don't ContainsKey chessTypeValue : {0}", chessTypeValue));
                }
            }           
            return chess;
        }

        // 初始化HASH，默认表示所有检查球上都没有棋子
        void InitHashIndexCheess ( ) {
            TraverseViewIndexSphere(( index ) => {
                if (!m_hashIndexCheess.ContainsKey(index)) {
                    m_hashIndexCheess.Add(index, null);
                }                
            });
        }

        /// <summary>
        /// 遍历所有VIEW下标检查球，执行Action
        /// </summary>
        /// <param name="action"></param>
        public void TraverseViewIndexSphere ( Action<int> action ) {
            for ( int index = 51; index <= 203; index++ ) {
                Util.CallAction<int>(action, index);
            }
        }

        public void SetHashIndexCheess ( int index, GameObject chess = null ) {
            if ( m_hashIndexCheess.ContainsKey(index) ) {
                m_hashIndexCheess[index] = chess;
            } else {
                Debuger.LogError(string.Format("Don't ContainsKey index : {0}", index));
            }
        }

        /// <summary>
        /// 得到该格子上的棋子GameObject，如果没棋子则为NULL
        /// </summary>
        /// <param name="index256"></param>
        /// <returns></returns>
        public GameObject GetHashIndexChess (int index256) {
            GameObject chess = null;
            if (m_hashIndexCheess.ContainsKey(index256)) {
                chess = m_hashIndexCheess[index256];
            } else {
                Debuger.LogError(string.Format("Don't ContainsKey index : {0}", index256));
            }
            return chess;
        }

        /// <summary>
        /// 初始所有棋子GameObject的位置
        /// </summary>
        public void InitAllChessPos ( ) {
            foreach ( KeyValuePair<int, GameObject> indexChess in m_hashIndexCheess ) {
                if ( null != indexChess.Value ) {
                    // 把棋子放到INDEX检测球的位置上
                    indexChess.Value.GetComponent<Transform>( ).localPosition = IndexCtrlBehaviour.instance.getIndexSphereGo(indexChess.Key).GetComponent<Transform>( ).localPosition;
                }
            }
        }

    }

}//end namespace

